<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%--<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>--%>
<%@ page import="java.util.List" %>
<%@ page import="com.webshop.domain.Items" %>
<%@ page import="com.webshop.Dao.PageDao" %><%--
  Created by IntelliJ IDEA.
  User: jxdck662
  Date: 2019/8/4
  Time: 20:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% response.setCharacterEncoding("UTF-8");%>
<%
    String search = request.getParameter("search");
    if (search==null){
        String pclass = request.getParameter("pclass");
        if (pclass == null){
            pclass = "1";
        }
        String n = request.getParameter("n");
        if (n==null){
            n="1";
        }
        int num = Integer.parseInt(n);
        String iclass = Items.allclass.get(Integer.parseInt(pclass));
        pageContext.setAttribute("pclass",iclass);
        int lenth = new PageDao().getItems(iclass).size();
        List<Items> items = new PageDao().getItems(iclass);
        if (items.size()>6){
            if (num*6<=lenth)
                items = items.subList((num-1)*6,num*6);
            else
            {
                items.add(new Items());
                items = items.subList((num-1)*6,lenth+1);
            }
        }
        pageContext.setAttribute(pclass,iclass);
        pageContext.setAttribute("pclassid",pclass);
        pageContext.setAttribute("items",items);
        pageContext.setAttribute("n",num);
        pageContext.setAttribute("max",(lenth%6==0?lenth/6:(lenth/6+1)));
    }
    if (search!=null){
        String n = request.getParameter("n");
        if (n==null){
            n="1";
        }
        int num = Integer.parseInt(n);
        String iclass = search;
        pageContext.setAttribute("pclass",iclass);
        List<Items> items = (List<Items>) request.getAttribute("items");
        int lenth = items.size();
        if (items.size()>6){
            if (num*6<=lenth)
                items = items.subList((num-1)*6,num*6);
            else
            {
                items.add(new Items());
                items = items.subList((num-1)*6,lenth+1);
            }
        }
        pageContext.setAttribute("items",items);
        pageContext.setAttribute("n",num);
        pageContext.setAttribute("max",(lenth%6==0?lenth/6:(lenth/6+1)));
    }
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
    <title></title>
</head>
<body style="background:url('./img/regist_bg.jpg'); background-repeat: repeat;">
<!-- header -->
<div class="container-fluid" style="background-color: white;">
    <div class="row">
        <div class="col-4"><img src="img/logo2.png" ></div>
        <div class="col-5"><img src="img/header.jpg" ></div>
        <div class="col">
            <c:if test="${sessionScope.user == null}">
                <div class="btn-group float-right" role="group" style="margin-top: 11px;">
                    <a href="LoginServlet"><button type="button" class="btn btn-outline-success">登录</button></a>
                    <a href="register.jsp"><button type="button" class="btn btn-outline-info">注册</button></a>
                    <a href="cart.jsp"><button type="button" class="btn btn-outline-secondary">购物车</button> </a>
                </div>
            </c:if>
            <c:if test="${sessionScope.user != null}">
                <div class="btn-group float-right" role="group" style="margin-top: 11px;">
                    <a href="GetUserServlet"><button type="button" class="btn btn-outline-success">${sessionScope.user.name}</button></a>
                    <a href="LogoutServlet"><button type="button" class="btn btn-outline-info">退出</button></a>
                    <a href="cart.jsp"><button type="button" class="btn btn-outline-secondary">购物车</button> </a>
                    <a href="./GetBusServlet"><button type="button" class="btn btn-outline-danger">我的订单</button> </a>
                </div>
            </c:if>
        </div>
    </div>
</div>
<!-- nav -->
<div class="container-fluid border border-primary" >
    <nav class="navbar navbar-dark bg-dark">
        <nav class="nav nav-pills">
            <c:forEach var="i" begin="0" end="${Items.size}">
                <a class="nav-link text-light" href="items.jsp?pclass=${i}&n=1">${Items.allclass[i]}</a>
            </c:forEach>
            <a class="nav-link text-light" href="index.jsp">首页</a>
        </nav>
        <form class="form-inline my-2 my-lg-0 " action="Search" method="post">
            <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search" name="search">
            <button class="btn btn-outline-light my-2 my-sm-0" type="submit">Search</button>
        </form>
    </nav>
</div>
<!-- title -->
<div class="container">
    <div class="row">
        <div class="col"><h1 style="margin-left: 50px;">${pclass}</h1></div>
        <div class="w-100"></div>
        <div class="col border-danger" style="background-color:rgba(45,12,19,0.3); padding: 50px 50px 50px 50px;">
            <div class="row">
                <c:forEach var="i" begin="0" end="5">
                        <c:if test="${items[i].pimgsrc ne null}">
                            <div class="col-sm-4">
                                <div class="card" style="width: 18rem; margin-bottom: 20px">
                                    <img class="card-img-top" src="${items[i].pimgsrc}" alt="image">
                                    <div style="border-bottom: 1px solid black; width: 100%"></div>
                                    <div class="card-body" style="background-color: rgba(154,136,120,0.1);">
                                        <h5 class="card-title">${items[i].pname}</h5>
                                        <p class="card-text">${items[i].sinfo}</p>
                                        <a href="product.jsp?id=${items[i].id}" class="btn btn-primary">去看看</a>
                                    </div>
                                </div>
                            </div>
                        </c:if>
                </c:forEach>
            </div>
        </div>
        <div class="w-100"></div>
        <div class="col text-center">
            <c:if test="${n ne 1}">
                <span><a href="items.jsp?pclass=${pclassid}&n=${n-1}">上一页</a></span>
                <span>${n-1}</span>
            </c:if>
            <span>${n}</span>
            <c:if test="${n ne max}">
                <span>${n+1}</span>
                <span><a href="items.jsp?pclass=${pclassid}&n=${n+1}">下一页</a></span>
            </c:if>
        </div>
    </div>
</div>
<!-- footer -->
<div class="container-fluid" >
    <nav class="nav justify-content-center">
        <a class="nav-link">关于我们</a>
        <a class="nav-link">联系我们</a>
        <a class="nav-link">招贤纳士</a>
        <a class="nav-link">法律声明</a>
        <a class="nav-link">友情链接</a>
        <a class="nav-link" target="_blank">支付方式</a>
        <a class="nav-link" target="_blank">配送方式</a>
        <a class="nav-link">服务声明</a>
        <a class="nav-link">广告声明</a>
    </nav>
</div>
<div style="text-align: center;margin-top: 5px;margin-bottom:20px;">
    Copyright &copy; 2005-2016 传智商城 版权所有
</div>
<!-- javascript -->
<script src="js/jquery-3.4.1.min.js"></script>
<script src="js/popper.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
